<!--
 * @Author: firesky
 * @Date: 2020-11-04 16:32:05
 * @LastEditTime: 2020-11-06 09:57:52
 * @LastEditors: firesky
-->
<template>
  <div class="app-container">
    <el-form v-loading="loadingForm" ref="form" :rules="rules" :model="form" label-width="140px">
      <el-form-item label="用户名" prop="username">
        <el-input :disabled="true" v-model="form.username"/>
      </el-form-item>
      <el-form-item label="原密码" prop="password">
        <el-input v-model="form.password" type="password"/>
      </el-form-item>
      <el-form-item label="新密码" prop="newpassword">
        <el-input v-model="form.newpassword" type="password"/>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loadingForm" type="primary" @click="confirmSubmit" >确认</el-button>
        <el-button @click="goBack">返回上一页面</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { common } from '../../mixins/common'
import { updatePass } from '../../api/login'

export default {
  mixins: [common],
  data() {
    return {
      loadingForm: false,
      form: {
        username: '',
        password: '',
        newpassword: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { min: 3, message: '长度大于3个字符', trigger: 'blur' }
        ],
        newpassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 3, message: '长度大于3个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    // console.log(this.globalDb)
    this.form.username = decodeURIComponent(this.globalDb.fname)
    this.form.db = this.globalDb.db
    this.form.type = 1
  },
  methods: {
    async confirmSubmit() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          // alert('submit!')
          try {
            const res = await updatePass(this.form)
            // console.log(res, '  res')
            if (res.Error === 0) {
              this.$message({
                message: res.Message,
                type: 'success'
              })
              this.goBack()
            } else {
              this.$message.error(res.Message)
            }
          } catch (error) {
            console.log(error)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    goBack() {
      this.$router.go(-1)
    }
  }
}
</script>
